package com.alogic.reload;

import com.alogic.load.Loadable;
import com.anysoft.util.*;
import org.w3c.dom.Element;

import java.util.Map;

/**
 * 重加载策略
 *
 * @since 1.6.13.5 [20200605 duanyy] <br>
 */
public interface Reloader extends Loadable {
    /**
     * 是否能够reload
     * @param id 对象id
     * @param loadable 对象
     * @return 是否可以reload
     */
    public boolean reload(String id,String domain,Loadable loadable);

    /**
     * 虚基类
     */
    public static abstract class Abstract extends Loadable.Abstract implements Reloader, XMLConfigurable, Configurable {
        protected String id;

        @Override
        public String getId() {
            return id;
        }

        @Override
        public boolean reload(String id, String domain,Loadable loadable) {
            return loadable == null ? true:loadable.isExpired();
        }

        @Override
        public void report(Element xml) {
            if (xml != null){
                XmlTools.setString(xml,"module",this.getClass().getName());
            }
        }

        @Override
        public void report(Map<String, Object> json) {
            if (json != null){
                JsonTools.setString(json,"module",this.getClass().getName());
            }
        }

        @Override
        public void configure(Properties p) {
            id = PropertiesConstants.getString(p,"id","",true);
        }

        @Override
        public void configure(Element e, Properties p) {
            XmlElementProperties props = new XmlElementProperties(e,p);
            configure(props);
        }
    }
}
